package coms.action;

import com.opensymphony.xwork2.Action;
import coms.delegates.PresentationTypeDelegate;
import coms.dto.PresentationType;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

public class PresentationTypeAction extends ComsActionSupport implements IAction {

    private int presentationTypeId;
    private PresentationType presentationType;
    private List<PresentationType> presentationTypeList;
    private PresentationTypeDelegate presentationTypeDelegate;
   
    private String action;
  
    
    public PresentationTypeAction() {
        presentationType = new PresentationType();
        presentationTypeDelegate = new PresentationTypeDelegate();
        presentationTypeList = new ArrayList<PresentationType>();

    }

    private void initForm() {
       
    }
    
     private void populateDefault() {
        presentationType = new PresentationType();
  
    }
    
    private void populate(){
        try {
            presentationType = presentationTypeDelegate.getById(presentationTypeId);
        } catch (Exception ex) {
            Logger.getLogger(PresentationTypeAction.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    @Override
    public String input() {

        initForm();
        populateDefault();
        action = "createPresentationType_";
        return "input";
    }

    @Override
    public String edit() {
        initForm();
        populate();
        action = "updatePresentationType_" + presentationType.getId();
        return "edit";
    }
    
    public String send(){
        populate();
        action = "sendPresentationType_" + presentationType.getId();
        return "send";
    }

    @Override
    public String create() {

        presentationTypeDelegate.create(presentationType);
        return Action.SUCCESS;
    }

    @Override
    public String update() {
        try {
             presentationTypeDelegate.update(presentationType);
        } catch (Exception ex) {
            Logger.getLogger(PresentationTypeAction.class.getName()).log(Level.SEVERE, null, ex);
        }
       
        return "updated";
    }

    @Override
    public String delete() {
         populate();
        presentationTypeDelegate.delete(presentationType);
        return "deleted";
    }

    @Override
    public String findAll() {
        try {
            presentationTypeList = presentationTypeDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(PresentationTypeAction.class.getName()).log(Level.SEVERE, null, ex);
        }
        return Action.SUCCESS;
    }

    @Override
    public String search() {
        try {
            presentationTypeList = presentationTypeDelegate.findAll();
        } catch (Exception ex) {
            Logger.getLogger(PresentationTypeAction.class.getName()).log(Level.SEVERE, null, ex);
        }
  
        return "search";
    }
    
    
    public PresentationType getPresentationType() {
        return presentationType;
    }

    
    public void setPresentationType(PresentationType presentationType) {
        this.presentationType = presentationType;
    }

    public List<PresentationType> getPresentationTypeList() {
        return presentationTypeList;
    }

    public void setPresentationTypeList(List<PresentationType> presentationTypeList) {
        this.presentationTypeList = presentationTypeList;
    }

    public void setPresentationTypeId(String presentationTypeId) {
       try{
       this.presentationTypeId = Integer.parseInt(presentationTypeId);
       presentationType.setId(this.presentationTypeId);
       } catch (Exception ex){
           Logger.getLogger(PresentationTypeAction.class.getName()).log(Level.SEVERE, null, ex);
       }
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }

    public String getPresentationTypeId() {
        return "" + presentationTypeId;
    }
    
}
